package com.jzy.stock.wareHoursePosition.controller;

import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import com.jzy.stock.util.StatusEnum;
import com.jzy.stock.util.XiaLaVo;
import com.jzy.stock.wareHoursePosition.model.*;
import com.jzy.stock.wareHoursePosition.service.WareHoursePositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@Api(value = "WareHoursePositionController", description = "库房库位功能")
public class WareHoursePositionController {

    @Autowired
    private WareHoursePositionService WareHoursePositionService;

    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo queryLegalPersonNameByUserId(String userName) {
        //通过用户登录的用户名查询用户的id，用id去查法人表
        int id = 1;
        return WareHoursePositionService.queryLegalPersonNameByUserId(id);
    }

    @ApiOperation(value = "分页接口")
    @PostMapping("StorageBin/queryPage")
    public ResultVo queryPage(@RequestBody SelectVo SelectVo) {
        return WareHoursePositionService.queryPage(SelectVo);
    }

    @ApiOperation(value = "新增接口")
    @PostMapping("StorageBin/add")
    public ResultVo add(@RequestBody  ListVo addVos, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return WareHoursePositionService.add(addVos);
    }

    @ApiOperation(value = "修改接口")
    @PostMapping("StorageBin/update")
    public ResultVo update(@RequestBody @Valid UpdateVo updateVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return WareHoursePositionService.update(updateVo);
    }

    @ApiOperation(value = "修改状态接口")
    @PostMapping("StorageBin/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid UpdateStatusVo updateStatusVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return WareHoursePositionService.updateStatus(updateStatusVo);
    }

    @ApiOperation(value = "状态下拉框接口")
    @PostMapping("iwm/getPullDownList")
    public ResultVo queryXiala(@RequestBody List<String> tmp) {
        Map<String, List<XiaLaVo>> stringListMap = new HashMap<>();
        Map<String, List<XiaLaVo>> returnMap = new HashMap<>();
        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (StatusEnum statusShengJIEnum : StatusEnum.values()) {
            XiaLaVo xiaLaVo = new XiaLaVo(statusShengJIEnum.getId(), statusShengJIEnum.getName());
            xiaLaVoList.add(xiaLaVo);
        }
        stringListMap.put("status", xiaLaVoList);
        if (CollectionUtils.isEmpty(tmp)) {
            return ResultBuildVo.success(stringListMap);
        } else {
            for (String qkey : tmp) {
                for (String key : stringListMap.keySet()) {
                    if (qkey.equals(key)) {
                        returnMap.put(key, stringListMap.get(key));
                    }
                }
            }
            return ResultBuildVo.success(returnMap);
        }
    }

    //库房下拉回显接口在库区controller中

    @GetMapping("StorageBin/queryStorageSection")
    public ResultVo queryStorageSection(Integer id) {
        return WareHoursePositionService.queryStorageSection(id);
    }

    @PostMapping("StorageBin/queryStorageBin")
    public ResultVo queryStorageBin(@RequestBody IwnVo iwnVo) {
        return WareHoursePositionService.queryStorageBin(iwnVo);
    }


    //库位导入
    @PostMapping("StorageBin/import")
    public ResultVo importFile(MultipartFile file)throws Exception{
        return WareHoursePositionService.importFile(file);
    }

    //库位导出
    @GetMapping("StorageBin/exportFile")
    public ResultVo exportFile(String code,String name,String storageSectionCode,String storageSectionName,String wareHouseCode,String wareHouseName,Boolean status, HttpServletResponse response)throws Exception{
        SelectVo selectVo = new SelectVo();
        selectVo.setCode(code);
        selectVo.setName(name);
        selectVo.setStorageSectionCode(storageSectionCode);
        selectVo.setStorageSectionName(storageSectionName);
        selectVo.setWareHouseCode(wareHouseCode);
        selectVo.setWareHouseName(wareHouseName);
        selectVo.setStatus(status);
        return WareHoursePositionService.exportFile(selectVo,response);
    }
}